﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using SuperSocket;
using SuperSocket.Command;
using System.Text;
using Example.Modbus.Command;
using Example.Modbus.Kernel;
using Xph.Sag.Kernel;

var builder = SuperSocketHostBuilder.Create<XphPackage, Crc16Filter>();
builder.UsePackageDecoder<XphDecoder>();//解码器

// 全局会话
builder.UseSession<RtuSession>()
    .UseSessionHandler(async (session) =>
    {
        Console.WriteLine($"客户端已连接: {session.RemoteEndPoint}");
        await session.SendAsync(Encoding.UTF8.GetBytes($"欢迎光临: {session.RemoteEndPoint} \r\n"));
    }, async (session, reason) =>
    {
        await Task.Delay(1000);
        Console.WriteLine($"客户端已断开: {session.RemoteEndPoint},{reason}");
    });


// 【路由】处理程序
builder.UseCommand((options) =>
{
    options.AddCommand<IdCommand>();
    options.AddCommand<ModbusDataCommand>();
});


var host = builder.Build();
await host.RunAsync();